package com.solocredit.finance.utils.helper

import com.solocredit.finance.BuildConfig
import com.solocredit.finance.app.MyApp
import com.solocredit.finance.config.MullaConfigs
import com.solocredit.finance.data.analyse.AnalyseBuilder
import com.solocredit.finance.data.analyse.AnalyseUtil
import com.solocredit.finance.data.dev.FlexiMolaDevHelper
import com.solocredit.finance.http.ApiService
import com.solocredit.finance.http.ApiService.addCommonParams
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.http.IHttpCallback
import org.json.JSONObject


/**
 * TODO: Analysis Lib 初始化时机
 */
object MullaAnalysisHelper {

    /**
     * TODO: APP启动时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtAppLaunch() {
        AnalyseBuilder.product = MullaConfigs.MULLA_APPNAME
        AnalyseBuilder.appVersion = BuildConfig.VERSION_CODE.toString()
        AnalyseBuilder.appPckName = MullaConfigs.MULLA_PACKAGENANME

        AnalyseBuilder.aesKey = MullaConfigs.MULLA_AES_KEY
        AnalyseBuilder.aesIv = MullaConfigs.MULLA_AES_IV

        AnalyseBuilder.host = ApiService.MULLA_BASE_URL
        AnalyseBuilder.zone = ""
        AnalyseBuilder.pathCommon = ApiService.API_URL.MULLA_API_COMMON_URL

        AnalyseBuilder.pathGoogleInstall = ApiService.ANALYSIS_PATH.MULLA_ANALYSIS_GOOGLE_INSTALL_PATH
        AnalyseBuilder.pathAdjustInstall = ApiService.ANALYSIS_PATH.MULLA_ANALYSIS_ADJUST_INSTALL_PATH
        AnalyseBuilder.pathFacebookInstall = ApiService.ANALYSIS_PATH.MULLA_ANALYSIS_FACEBOOK_INSTALL_PATH
        AnalyseBuilder.pathFirebaseInstall = ApiService.ANALYSIS_PATH.MULLA_ANALYSIS_FIREBASE_INFO_PATH

        AnalyseUtil.init(MyApp.app)
    }

    /**
     * TODO: 成功获取地区转发路径时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtZoneSuccess() {
        AnalyseBuilder.zone = SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_TIMEZONE)
        AnalyseUtil.init(MyApp.app)
    }

    /**
     * TODO: 成功登录时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtLoginSuccess() {
        AnalyseBuilder.token = SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_TOKEN)
        AnalyseBuilder.userId = SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_USERID)
        AnalyseUtil.init(MyApp.app)
    }
}

/**
 * TODO: Analysis Lib 打点时机
 */
object TestAnalysisAddEvent {

    /**
     * TODO: 在调用登录接口，并成功返回时，如果 type=="REGISTER"，则调用注册事件打点
     */
    fun addRegisterEvent() {
        AnalyseUtil.addRegisterEvent()
    }

    /**
     * TODO: 在调用提交贷款申请接口，并成功返回时，则调用申请事件打点
     */
    fun addApplyEvent() {
        AnalyseUtil.addApplyEvent()
    }
}

/**
 * TODO: Data Lib 调用时机
 */
object TestDataLibInvoke {

    /**
     * 获取设备数据上传状态
     */
    fun takeFlexiMolaDeviceInfoTypeAction(){
        val params = JSONObject().addCommonParams()
        val httpUrl = ApiService.getHttpUrl(ApiService.API_PATH.MULLA_API_DEVICE_INFO_TYPES_PATH)
        HttpHelper.postMullaAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        obj?.let {
                            val deviceInfoTypes = obj.optString("deviceInfoTypes")
                            FlexiMolaDevHelper.onFlexiMolaReportDevData(MyApp.app,deviceInfoTypes)
                        }
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
            }
        })
    }
}